#include "opendatawizard.h"
#include "ui_opendatawizard.h"

OpenDataWizard::OpenDataWizard(QWidget *parent) :
    QWizard(parent),
    m_ui(new Ui::OpenDataWizard)
{
    m_ui->setupUi(this);
    connect(this,SIGNAL(accepted()),this,SLOT(onaccepted()));
    connect(this,SIGNAL(currentIdChanged(int)),this,SLOT(oncurrentIdChanged(int)));

#ifdef _DEBUG
	m_ui->corrMatrixFileEdit->setText(tr("%1/RunsmoothedSubjs.mat").arg(DATA_ROOT));
    m_ui->meanMatrixEdit->setText(tr("%1/Data/exampleMat.mat").arg(DATA_ROOT));
    m_ui->medianMatrixEdit->setText(tr("%1/").arg(DATA_ROOT));
#else
	m_ui->corrMatrixFileEdit->setText(QDir::currentPath()+"/Data/RunsmoothedSubjs.mat");
	m_ui->meanMatrixEdit->setText(QDir::currentPath()+"/Data/exampleMat.mat");
	m_ui->medianMatrixEdit->setText(QDir::currentPath()+"/Data/");
#endif

    loadPage = new LoaderWizardPage(this);
    finishPage = new QWizardPage(this);
    finishPage->setTitle(tr("Finished"));
    finishPage->setSubTitle(tr("Finished loading the dataset. Click Finish to open the dataset in the application."));

    m_ui->wizardPage2->setCommitPage(true);
    addPage(loadPage);
    addPage(finishPage);
	m_ui->browseMeanMatrixButton->setVisible(false);
	m_ui->meanMatrixEdit->setVisible(false);
	m_ui->browseMedianMatrixButton->setVisible(false);
	m_ui->medianMatrixEdit->setVisible(false);
    setModal(true);
}

OpenDataWizard::~OpenDataWizard()
{
    delete m_ui;
}

void OpenDataWizard::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void OpenDataWizard::oncurrentIdChanged(int id)
{
    if(id<3)
    {        
		loadPage->setIndividualsMatrixFile(m_ui->corrMatrixFileEdit->text());
		//if(!m_ui->calculateMeanFromSetCheck->isChecked())
#ifdef _DEBUG
	QString fPath(tr(DATA_ROOT)+tr("/aal.csv"));
#else
	QString fPath(QDir::currentPath()+"/Data/aal.csv");
#endif

		loadPage->setMeanMatrixFile(fPath); //m_ui->meanMatrixEdit->text());
		//else
		//	loadPage->setMeanMatrixFile(tr(""));
		//if(!m_ui->calculateMedianFromSetCheck->isChecked())
		//	loadPage->setMedianMatrixFile(m_ui->medianMatrixEdit->text());
		//else
		//	loadPage->setMedianMatrixFile(tr(""));
    }
	if(id==1)
		next();
}

void OpenDataWizard::on_browseMatrixButton_clicked()
{
    m_ui->corrMatrixFileEdit->setText(QFileDialog::getOpenFileName(this, tr("Open Individuals Matrix"), "~/", tr("Matlab Matrix Files (*.mat)")));
}

void OpenDataWizard::on_calculateMeanFromSetCheck_toggled(bool on)
{
	m_ui->meanMatrixEdit->setVisible(!on);
	m_ui->browseMeanMatrixButton->setVisible(!on);
}

void OpenDataWizard::on_calculateMedianFromSetCheck_toggled(bool on)
{
	m_ui->medianMatrixEdit->setVisible(!on);
	m_ui->browseMedianMatrixButton->setVisible(!on);
}

void OpenDataWizard::on_browseMeanMatrixButton_clicked()
{
	m_ui->meanMatrixEdit->setText(QFileDialog::getOpenFileName(this, tr("Open Reference Matrix (mean)"), "~/", tr("Matlab Matrix Files (*.mat)")));
}

void OpenDataWizard::on_browseMedianMatrixButton_clicked()
{
	m_ui->medianMatrixEdit->setText(QFileDialog::getOpenFileName(this, tr("Open Reference Matrix (median)"), "~/", tr("Matlab Matrix Files (*.mat)")));
}

void OpenDataWizard::onaccepted()
{

}

AnatomicalTemplate *OpenDataWizard::GetTemplate()
{
    return loadPage->GetTemplate();
}

QVector<CorrelationMatrix*>& OpenDataWizard::GetIndividualsMatrices() const
{
	return loadPage->GetIndividualsMatrices();
}

CorrelationMatrix* OpenDataWizard::GetMeanMatrix()
{
	return loadPage->GetMeanMatrix();
}

CorrelationMatrix* OpenDataWizard::GetMedianMatrix()
{
	return loadPage->GetMedianMatrix();
}

FunctionalNetwork *OpenDataWizard::GetNetwork()
{
    return loadPage->GetNetwork();
}
